package com.microstation.spring.config;

import com.microstation.realm.MyRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Shiro 配置注入类
 */
@Configuration
@AutoConfigureAfter(MyRealm.class)
public class ShiroConfig {
//    @Autowired
//    private MyRealm myRealm;

    /**
     * 安全管理器
     *
     * @return
     */
    @Bean(name = "securityManager")
    public SecurityManager securityManager(MyRealm myRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm);
        return securityManager;
    }

    /**
     * Shiro过滤器
     *
     * @return
     */
    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //Shiro的核心安全接口,这个属性是必须的
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //身份认证失败，则跳转到登录页面的配置
        shiroFilterFactoryBean.setLoginUrl("../adm/login.html");
        //权限认证失败，则跳转到指定页面
        shiroFilterFactoryBean.setUnauthorizedUrl("../authenticationError.html");
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/api/login/**", "anon");
        filterChainDefinitionMap.put("/static_pc/**", "anon");
        filterChainDefinitionMap.put("/adm/login.html", "anon");
        filterChainDefinitionMap.put("/authenticationError.html", "anon");
        filterChainDefinitionMap.put("/error.html", "anon");
        filterChainDefinitionMap.put("/timeout.html", "anon");
        filterChainDefinitionMap.put("/adm/assets/**", "anon");
        filterChainDefinitionMap.put("/adm/js/**", "anon");
        filterChainDefinitionMap.put("/upload/**", "anon");
        filterChainDefinitionMap.put("/uploadfile/**", "anon");
        filterChainDefinitionMap.put("/uploadshow/**", "anon");
        filterChainDefinitionMap.put("/uploadstart/**", "anon");
        filterChainDefinitionMap.put("/api/admin/**", "authc");
        filterChainDefinitionMap.put("/api/menu/**", "authc");
        filterChainDefinitionMap.put("/api/auth/**", "authc");
        filterChainDefinitionMap.put("/api/ques/**", "authc");
        filterChainDefinitionMap.put("/api/imgInfo/**", "authc");
        //filterChainDefinitionMap.put("/api/userPrize/**", "authc");
        //filterChainDefinitionMap.put("/api/prizeInfo/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 保证实现了Shiro内部lifecycle函数的bean执行
     *
     * @return
     */
    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    /**
     * DefaultAdvisorAutoProxyCreator，Spring的一个bean，由Advisor决定对哪些类的方法进行AOP代理。
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true); // this SETTING
        return proxyCreator;
    }

    /**
     * AuthorizationAttributeSourceAdvisor，shiro里实现的Advisor类，
     * 内部使用AopAllianceAnnotationsAuthorizingMethodInterceptor来拦截用以下注解的方法。
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
